package com.symbio.ishenapi.background.controller;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.background.model.TmDrug;
import com.symbio.ishenapi.background.model.TmDrugBase;
import com.symbio.ishenapi.background.model.TmNews;
import com.symbio.ishenapi.background.model.base.BaseTmDrug;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 药品管理
 */
@RequestMapping("/TmDrug")
public class TmDrugController extends BaseController {

    /**
     * wy:根据处方大类和处方类型
     *      查询药品所有信息
     */
    public void findDrugAll(){
        TmDrug tmDrug = (TmDrug) getReqBodyObject(TmDrug.class);
        List<TmDrug> list = tmDrug.dao().findDrugAll(tmDrug);
        if (list != null && list.size() > 0){
            renderJsonRet(list);
        }else {
            renderJsonError(40003,"请重试!");
        }
    }
    /**
     * 药品列表分页展示
     */
    public void findPageAll(){
        TmDrug tmDrug = new TmDrug();
        String drugName = getPara("drugName");
        Map map = tmDrug.dao().findPageAll(getQueryModel(),drugName);
        if (null != map && map.size() > 0){
            renderPageJsonRet(map.get("count"),map.get("data"));
        }else {
            renderJsonError(40003,"请重试!");
        }
    }

    /**
     * 药品修改之回显
     */
    public void findById(){
        TmDrug tmDrug = (TmDrug)getReqBodyObject(TmDrug.class);
        if (null == tmDrug.getId() && "".equals(tmDrug.getId())){
            renderJsonError(40005,"请重试!");
            return;
        }else {
            renderJsonRet(tmDrug.findById(tmDrug.getId()));
        }
    }


    /**
     * 药品批量禁用
     */
    public void delBatchDrug(){
        String ids = getReqBodyString().replace("\"","").replace("\"","");
        System.out.println(ids);
        TmDrug tmDrug = new TmDrug();
        if (tmDrug.delBatchDrug(ids)){
            renderJsonRet(40004);
        }else {
            renderJsonError(40009,"");
        }
    }

    /**
     * 药品禁用
     */
    public void delDrug(){
        TmDrug tmDrug = (TmDrug)getReqBodyObject(TmDrug.class);
        if (null == tmDrug.getId() && "".equals(tmDrug.getId())){
            renderJsonError(40009,"");
            return;
        }else {
            tmDrug.update();
            renderJsonRet(40004);
        }
    }

    /**
     * 药品添加
     */
    public void saveDrug(){
        TmDrug tmDrug = (TmDrug)getReqBodyObject(TmDrug.class);
        tmDrug.setStatus("1");
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tmDrug.setCreateTime(sdf.format(dt));
        if (null == tmDrug.getDrugName() && "".equals(tmDrug.getDrugName())){
            renderJsonError(40003,"药品名称");
        }else if (null == tmDrug.getUseName() && "".equals(tmDrug.getUseName())){
            renderJsonError(40003,"通用名!");
        }else if (null == tmDrug.getManufacturerName() && "".equals(tmDrug.getManufacturerName())){
            renderJsonError(40003,"药品名称(厂家)");
        }else if (null == tmDrug.getDrugCode() && "".equals(tmDrug.getDrugCode())){
            renderJsonError(40003,"药品编码");
        }else if (null == tmDrug.getSpec() && "".equals(tmDrug.getSpec())){
            renderJsonError(40003,"规格");
        }else if (null == tmDrug.getStorage() && "".equals(tmDrug.getStorage())){
            renderJsonError(40003,"药库");
        }else if (null == tmDrug.getManufacturers() && "".equals(tmDrug.getManufacturers())){
            renderJsonError(40003,"厂家");
        }else if (null == tmDrug.getStock() && "".equals(tmDrug.getStock())){
            renderJsonError(40003,"库存数量");
        }else if (null == tmDrug.getSalePrice() && "".equals(tmDrug.getSalePrice())){
            renderJsonError(40003,"售价");
        }else {
            if (null != tmDrug.getId() && !"".equals(tmDrug.getId())){
                if (tmDrug.update()){
                    renderJsonRet(40001);
                }else {
                    renderJsonError(40002,"");
                }
            }else {
                if (tmDrug.save()){
                    renderJsonRet(40004);
                }else {
                    renderJsonError(40009,"");
                }
            }
        }
    }
}
